
//*江南吴彦祖制作
const 根目录 = "/sdcard/脚本/"
var jsFiles = new Array();
jsName = new Array();
thelist(根目录)

function thelist(str) {
    var halfway = files.listDir(str, function(name) {
        if (name.indexOf(".") == -1) {
            var fileFile = str + name + "/"
            thelist(fileFile);
        }
        return name.endsWith(".js")
    })
    for (i = 0; i < halfway.length; i++) {
        jsFiles = jsFiles.concat(str + halfway[i])
        jsName = jsName.concat(halfway[i])
    }
}

function closeJs() {
    var my = engines.myEngine();
    all = engines.all();
    for (var i = 0; i < all.length; i++) {
        if (all[i] != my) {
            all[i].forceStop();
        }
    }
}

var window = floaty.window('<frame gravity="center " padding="1"><button id="mode" text="长按退出,点击切歌" padding="1" w="60" h="60" textSize="10"/></frame>');

setInterval(() => {}, 1000)
dh = device.height * 8 / 10;
dw = device.width / 5
window.setPosition(dh, dw);
window.exitOnClose();
window.mode.on("click", () => {
    closeJs();
    threads.start(function() {
        var filters = prompt("搜索")

        myname = jsName.filter(function(name) {
            return name.indexOf(filters) != -1
        })
        myname = myname[dialogs.singleChoice("选择文件", myname)];
        mySong = jsFiles.filter(function(name) {
            return name.indexOf(myname) != -1
        })
        if (mySong.length == 1) {
            engines.execScriptFile(mySong);
        } else {
            mySong = mySong[dialogs.singleChoice("选择文件", mySong)]
            engines.execScriptFile(mySong);
        }
    })
});
window.mode.on("long_click", () => {
    exit()
})
